package com.hq.hotl.controller;

import com.hq.hotl.pojo.Customer;
import com.hq.hotl.pojo.PageData;
import com.hq.hotl.pojo.User;
import com.hq.hotl.service.UserService;
import net.sf.jsqlparser.statement.alter.Alter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userService;
    /*
     * 管理员登录
     * */
    @RequestMapping("/userlogin")
    public String userlogin(String username, String password, ModelMap modelMap){
        if (username ==null || password == null){
            return "false";
        }
        User user = userService.userlogin(username, password);
        if (user.equals("登录失败")){
            return "false";
        }
        modelMap.put("user",user);
        return "usersystem";
    }


    @RequestMapping("/list2")
    public String list2(){
        return "list2";
    }

    @RequestMapping("/list4")
    public String list4(){
        return "list4";
    }

    @RequestMapping("/list3")
    public String list3(){
        return "list3";
    }

    @RequestMapping("/list1")
    public String list1(Integer currentPage,Integer pageSize, ModelMap modelMap){
        if (currentPage==null){
            currentPage=1;
        }
        if (pageSize==null){
            pageSize=5;
        }
        //直接调用业务层实现业务，获取到模型数据
        PageData pageData = userService.getAllByPage(currentPage, pageSize);
        //使用modeMap对象共享数据，给jsp页面传递上下文数据
        modelMap.put("pageData",pageData);
        return "list1";
    }

    @RequestMapping("/deleteuuOne")
    public String deleteOne(int id,Integer currentPage,Integer pageSize){
        userService.deleteuser(id);
        return "redirect:/list1?currentPage="+currentPage+"&pageSize="+pageSize;

    }

    //获取到传入的id的全部信息
    @RequestMapping("/queryuuOne")
    public String queryOne(String username,ModelMap modelMap){
        User user = userService.getOne(username);
        modelMap.put("user",user);
        return "updateuser";
    }

    //修改customer信息
    @RequestMapping("/updateuser")
    public String updateCustomer(User user,Integer currentPage,Integer pageSize){
                userService.updatauser(user);
            return "redirect:/list1?currentPage="+1+"&pageSize="+5;
        }

    @RequestMapping("/list6")
    public String list6(){
        return "list6";
    }

    @RequestMapping("/list7")
    public String list7(){
        return "list7";
    }

    @RequestMapping("/list8")
    public String list8(User user,ModelMap modelMap){
        modelMap.put("user",user);
        return "list8";
    }
    @RequestMapping("/list9")
    public String list9(){
        return "list9";
    }


    @RequestMapping("/updatauser")
    public String updatauser(User user,ModelMap modelMap){
        String message="修改成功";
        System.out.println(user);
        userService.updatauser(user);
        modelMap.put("message",message);
        return "false";
    }

    @RequestMapping("/logout")
    @ResponseBody
    public String logout(ModelMap modelMap){
        Map<String, Object> logout = userService.logout();
        if (logout.values().equals("登出成功")){
            modelMap.put("message",logout.values());
            return "false";
        }
        modelMap.put("message","登出成功");
        return "false";
    }

 @RequestMapping("/hygl")
    public String hygl(){
        return "hygl";
 }

 @RequestMapping("/xiaolianjsp")
    public String xiaolianjsp(){

        return "xiaolianjsp";
 }

}
